<?php

include 'DBconn.php';

class TransactionNote {

    public function addNote($VObject) {
        $note_code = "";
        if ($VObject->note_type == "Credit") {
            $note_code = $this->createCreditNoteCode();
        } else if ($VObject->note_type == "Debit") {
            $note_code = $this->createDebitNoteCode();
        }
        $query = "insert into trans_note_header(note_code,client_serialnumber_fk,trans_type,note_type,note_description,note_date,cre_userid_fk,cre_datetime,activestatus) values('$note_code','$VObject->client_serialnumber_fk','$VObject->trans_type','$VObject->note_type','$VObject->note_description','$VObject->note_date','$VObject->cre_userid_fk','$VObject->cre_datetime',1)";
        $s = mysql_query($query) or die(mysql_error());
        if ($s) {
            $last_id = mysql_insert_id();
            foreach ($VObject->pro_serialnumber_fk as $r => $value) {
                $trans_serialnumber_fk = mysql_real_escape_string($VObject->trans_serialnumber_fk[$r]);
                $quantity = mysql_real_escape_string($VObject->quantity[$r]);
                $trans_qty = mysql_real_escape_string($VObject->trans_qty[$r]);
                $unit_price = mysql_real_escape_string($VObject->unit_price[$r]);
                $discount_or_tax = mysql_real_escape_string($VObject->discount_or_tax[$r]);
                $price = mysql_real_escape_string($VObject->price[$r]);
                $remarks = mysql_real_escape_string($VObject->remarks[$r]);
                $query2 = "insert into trans_note_pro_details(trans_note_header_fk,trans_serialnumber_fk,pro_serialnumber_fk,quantity,trans_qty,unit_price,discount_or_tax,price,remarks,cre_userid_fk,cre_datetime,activestatus) values('$last_id','$trans_serialnumber_fk','$value','$quantity','$trans_qty','$unit_price','$discount_or_tax','$price','$remarks','$VObject->cre_userid_fk','$VObject->cre_datetime',1)";
                mysql_query($query2) or die(mysql_error());
            }
        }
        return "true";
    }

    public function createCreditNoteCode() {
        $query = "select credit_code from credit_code_table  order by credit_code desc limit 1 ";
        $result = mysql_query($query) or die(mysql_error());
        if (mysql_num_rows($result)) {
            $row = mysql_fetch_array($result);
            $str = explode("CR", $row[0]);
            $num = intval($str[1]);
            $res = "CR" . ++$num;
        } else {
            $res = 'CR1001';
        }
        $query = "insert into credit_code_table(credit_code) values('$res')";
        $result = mysql_query($query) or die(mysql_error());
        return $res;
    }

    public function createDebitNoteCode() {
        $query = "select debit_code from debit_code_table  order by debit_code desc limit 1 ";
        $result = mysql_query($query) or die(mysql_error());
        if (mysql_num_rows($result)) {
            $row = mysql_fetch_array($result);
            $str = explode("DE", $row[0]);
            $num = intval($str[1]);
            $res = "DE" . ++$num;
        } else {
            $res = 'DE1001';
        }
        $query = "insert into debit_code_table(debit_code) values('$res')";
        $result = mysql_query($query) or die(mysql_error());
        return $res;
    }

    public function getAllBuyingNotes() {
        $ret = array();
        $query = "select * from trans_note_header where trans_type='IN' and activestatus=1";
        $s = mysql_query($query) or die(mysql_error());
        while ($row = mysql_fetch_object($s)) {
            $temp = new VoTransactionNote();
            $temp->serialnumber = (int) $row->serialnumber;
            $temp->note_code = $row->note_code;
            $temp->client_serialnumber_fk = (int) $row->client_serialnumber_fk;
            $temp->trans_type = $row->trans_type;
            $temp->note_type = $row->note_type;
            $temp->note_description = $row->note_description;
            $temp->note_date = date("d-m-Y", strtotime($row->note_date));
            $temp->cre_userid_fk = (int) $row->cre_userid_fk;
            $temp->cre_datetime = $row->cre_datetime;
            $temp->activestatus = (boolean) $row->activestatus;

            $clientresult = mysql_query("select client_name from client_header where serialnumber='$row->client_serialnumber_fk' and activestatus=1") or die(mysql_error());
            ;
            while ($clientrow = mysql_fetch_object($clientresult)) {
                $temp->client_name = $clientrow->client_name;
            }

            $query2 = "select * from trans_note_pro_details where trans_note_header_fk='$row->serialnumber' and activestatus=1";
            $s2 = mysql_query($query2) or die(mysql_error());
            while ($row2 = mysql_fetch_object($s2)) {
                $temp->detserialnumber[] = (int) $row2->detserialnumber;
                $temp->trans_note_header_fk[] = (int) $row2->trans_note_header_fk;
                $temp->trans_serialnumber_fk[] = (int) $row2->trans_serialnumber_fk;
                $temp->pro_serialnumber_fk[] = (int) $row2->pro_serialnumber_fk;
                $temp->quantity[] = (int) $row2->quantity;
                $temp->trans_qty[] = (int) $row2->trans_qty;
                $temp->unit_price[] = $row2->unit_price;
                $temp->discount_or_tax[] = $row2->discount_or_tax;
                $temp->price[] = (int) $row2->price;
                $temp->remarks[] = $row2->remarks;
            }
            $ret[] = $temp;
            mysql_free_result($s2);
        }
        mysql_free_result($s);
        return $ret;
    }

    public function getAllSalesNotes() {
        $ret = array();
        $query = "select * from trans_note_header where trans_type='OUT' and activestatus=1";
        $s = mysql_query($query) or die(mysql_error());
        while ($row = mysql_fetch_object($s)) {
            $temp = new VoTransactionNote();
            $temp->serialnumber = (int) $row->serialnumber;
            $temp->note_code = $row->note_code;
            $temp->client_serialnumber_fk = (int) $row->client_serialnumber_fk;
            $temp->trans_type = $row->trans_type;
            $temp->note_type = $row->note_type;
            $temp->note_description = $row->note_description;
            $temp->note_date = date("d-m-Y", strtotime($row->note_date));
            $temp->cre_userid_fk = (int) $row->cre_userid_fk;
            $temp->cre_datetime = $row->cre_datetime;
            $temp->activestatus = (boolean) $row->activestatus;

            $clientresult = mysql_query("select client_name from client_header where serialnumber='$row->client_serialnumber_fk' and activestatus=1") or die(mysql_error());
            ;
            while ($clientrow = mysql_fetch_object($clientresult)) {
                $temp->client_name = $clientrow->client_name;
            }

            $query2 = "select * from trans_note_pro_details where trans_note_header_fk='$row->serialnumber' and activestatus=1";
            $s2 = mysql_query($query2) or die(mysql_error());
            while ($row2 = mysql_fetch_object($s2)) {
                $temp->detserialnumber[] = (int) $row2->detserialnumber;
                $temp->trans_note_header_fk[] = (int) $row2->trans_note_header_fk;
                $temp->trans_serialnumber_fk[] = (int) $row2->trans_serialnumber_fk;
                $temp->pro_serialnumber_fk[] = (int) $row2->pro_serialnumber_fk;
                $temp->quantity[] = (int) $row2->quantity;
                $temp->trans_qty[] = (int) $row2->trans_qty;
                $temp->unit_price[] = $row2->unit_price;
                $temp->discount_or_tax[] = $row2->discount_or_tax;
                $temp->price[] = (int) $row2->price;
                $temp->remarks[] = $row2->remarks;
            }
            $ret[] = $temp;
            mysql_free_result($s2);
        }
        mysql_free_result($s);
        return $ret;
    }

    public function getNoteByCode($serialnumber) {
        $ret = array();
        $query = "select * from trans_note_header where serialnumber='$serialnumber' and activestatus=1";
        $s = mysql_query($query) or die(mysql_error());
        $row = mysql_fetch_object($s);
        $temp = new VoTransactionNote();
        $temp->serialnumber = (int) $row->serialnumber;
        $temp->note_code = $row->note_code;
        $temp->client_serialnumber_fk = (int) $row->client_serialnumber_fk;
        $temp->trans_type = $row->trans_type;
        $temp->note_type = $row->note_type;
        $temp->note_description = $row->note_description;
        $temp->note_date = $row->note_date;
        $temp->cre_userid_fk = (int) $row->cre_userid_fk;
        $temp->cre_datetime = $row->cre_datetime;
        $temp->activestatus = (boolean) $row->activestatus;
        $query2 = "select * from trans_note_pro_details where trans_note_header_fk='$row->serialnumber' and activestatus=1";
        $s2 = mysql_query($query2) or die(mysql_error());
        while ($row2 = mysql_fetch_object($s2)) {
            $temp->detserialnumber[] = (int) $row2->detserialnumber;
            $temp->trans_note_header_fk[] = (int) $row2->trans_note_header_fk;
            $temp->trans_serialnumber_fk[] = (int) $row2->trans_serialnumber_fk;
            $temp->pro_serialnumber_fk[] = (int) $row2->pro_serialnumber_fk;
            $temp->quantity[] = (int) $row2->quantity;
            $temp->trans_qty[] = (int) $row2->trans_qty;
            $temp->unit_price[] = $row2->unit_price;
            $temp->discount_or_tax[] = $row2->discount_or_tax;
            $temp->price[] = (int) $row2->price;
            $temp->remarks[] = $row2->remarks;
        }
        $ret[] = $temp;
        mysql_free_result($s2);
        mysql_free_result($s);
        return $ret;
    }

}

class VoTransactionNote {

    var $serialnumber;
    var $note_code;
    var $client_serialnumber_fk;
    var $client_name;
    var $trans_serialnumber_fk;
    var $trans_type;
    var $note_type;
    var $note_description;
    var $note_date;
    var $cre_userid_fk;
    var $cre_datetime;
    var $activestatus;
    var $detserialnumber;
    var $trans_note_header_fk;
    var $pro_serialnumber_fk;
    var $quantity;
    var $trans_qty;
    var $unit_price;
    var $discount_or_tax;
    var $price;
    var $remarks;

}

?>